package mirrormonkey.core.client;

import mirrormonkey.core.messages.EntityEndMessage;
import mirrormonkey.framework.EntityProvider;

import com.jme3.network.Message;
import com.jme3.network.MessageConnection;
import com.jme3.network.MessageListener;

public class EntityEndListener implements MessageListener<MessageConnection> {

	public final EntityProvider entityProvider;

	public final ClientCoreModule module;

	public EntityEndListener(EntityProvider entityProvider,
			ClientCoreModule module) {
		this.entityProvider = entityProvider;
		this.module = module;
	}

	@Override
	public void messageReceived(MessageConnection source, Message message) {
		EntityEndMessage castMessage = (EntityEndMessage) message;
		ClientEntityData data = module.getData(castMessage.entityId);
		data.referenceDeparted();
		if (module.getData(source).isRunning() && !entityProvider.hasEntities()) {
			module.getData(source).setRunning(false);
		}
	}

}
